package com.zk.fm.util;

import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class WebServiceUtil {

	private static final String NAMESPACE = "http://fm.zk.com/";
	private static final String URL = "http://zhengkang0301.xicp.net:8080/FMToolsWS/ManagerPort?wsdl";

	public static final String METHOD_GETNEWESTVERSION = "getNewestVersion";
	public static final String METHOD_LOGIN = "login";
	public static final String METHOD_REGISTER = "register";
	public static final String METHOD_SUGGEST = "suggest";
	public static final String METHOD_GETALLPLAYERS = "getAllPlayers";
	public static final String METHOD_GETPLAYERBYNAME = "getPlayersByName";

	public static String callWebService(String method, List<Object> arg,
			int timeout) throws Exception {
		SoapObject rpc = new SoapObject(NAMESPACE, method);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		if (arg != null && !arg.isEmpty()) {
			int size = arg.size();
			for (int i = 0; i < size; i++) {
				rpc.addProperty("arg" + i, arg.get(i));
			}
		}
		envelope.bodyOut = rpc;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		HttpTransportSE ht = new HttpTransportSE(URL, timeout);
		ht.debug = true;
		ht.call(NAMESPACE + method, envelope);
		Object detail = envelope.getResponse();
		return detail.toString();
	}

	public static SoapObject callwebService4Obj(String method,
			List<String> arg, int timeout) throws Exception {
		SoapObject rpc = new SoapObject(NAMESPACE, method);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		if (arg != null && !arg.isEmpty()) {
			int size = arg.size();
			for (int i = 0; i < size; i++) {
				rpc.addProperty("arg" + i, arg.get(i));
			}
		}
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		HttpTransportSE ht = new HttpTransportSE(URL, 10000);
		ht.debug = true;

		ht.call(NAMESPACE + method, envelope);

		if (envelope.getResponse() == null) {
			return null;
		}
		SoapObject rpsObject = (SoapObject) envelope.bodyIn;
		return rpsObject;

	}
}
